#include <my_head.h>
#define N 10
typedef struct _Node{
    int num;
}node_t;


typedef struct _List
{
    node_t data[N];
    int count;
}list_t;

//创建顺序表
int creat_list(list_t **p){
    if(NULL == p){
        printf("入参为空,错误\n");
        return -1;
    }
    *p = (list_t *)malloc(sizeof(list_t));
    if(NULL == *p){
        return -1;
    }
    (*p)->count = 0;
    return 0;
}

//打印元素
int print_list(list_t *p){
    if(NULL == p){
        return -1;
    }
    int i = 0;
    for(i = 0;i<p->count;i++){
        printf("%d ",p->data[i].num);
    }
    printf("\n");
    return 0;
}

//向顺序表中插入元素（尾插）
int insert_list_by_tail(list_t *p,int value){
    if(NULL == p){
        return -1;
    }
    if(N == p->count){
        return -1;
    }
    p->data[p->count].num = value;
    p->count++;
    return 0;
}

//任意位置插入
int insert_list_by_position(list_t *p,int position,int value){
    if(NULL == p){
        return -1;
    }
    if(N == p->count){
        return -1;
    }
    if(position<0 || position>p->count){
        return -1;
    }
    int i = 0;
    for(i = p->count-1;i>=position;i--){
        p->data[i+1] = p->data[i];
    }
    p->data[position].num = value;
    p->count++;
    return 0;
}

//尾删
int delete_list_by_tail(list_t *p){
    if(NULL == p){
        return -1;
    }
    if(0 == p->count){
        return -1;
    }
    p->count--;
    return 0;
}

//任意位置删除
int delete_list_by_position(list_t *p,int position){
    if(NULL == p){
        return -1;
    }
    if(0 == p->count){
        return -1;
    }
    if(0 > position || position>=p->count){
        return -1;
    }
    int i = 0;
    for(i = position;i<p->count-1;i++){
        p->data[i] = p->data[i+1];
    }
    p->count--;
    return 0;
}

//修改指定位置元素
int modify_list_by_position(list_t *p,int position,int new_value){
    if(NULL == p){
        return -1;
    }
    if(0>position || position>=p->count){
        return -1;
    }
    p->data[position].num = new_value;
    return 0;
}

//剔重

int tichong_list(list_t *p){
    if(NULL == p){
        return -1;
    }
    int i = 0;
    int j = 0;
    int k = 0;
    for(i = 0;i<p->count-1;i++){
        for(j = i+1;j<p->count;){
            if(p->data[i].num == p->data[j].num){
                for(k = j+1;k<p->count;k++){
                    p->data[k-1] = p->data[k];
                }
                p->count--;
            }else{
                j++;
            }
        }
    }
    return 0;
}


int main(int argc, char const *argv[])
{
    list_t *my_list = NULL;
    creat_list(&my_list);
    insert_list_by_tail(my_list ,10);
    insert_list_by_tail(my_list ,20);
    insert_list_by_tail(my_list ,30);
    insert_list_by_tail(my_list ,40);
    print_list(my_list);


    insert_list_by_position(my_list,3,20);
    insert_list_by_position(my_list,2,60);
    insert_list_by_position(my_list,1,20);
    print_list(my_list);


    delete_list_by_tail(my_list);
    print_list(my_list);

    delete_list_by_position(my_list,3);
    print_list(my_list);

    tichong_list(my_list);
    print_list(my_list);
    return 0;
}




